package controllers

import (
	"2302a_news/models"
	beego "github.com/beego/beego/v2/server/web"
)

type NewsController struct {
	beego.Controller
}

func (c *NewsController) Create() {
	var err error
	title := c.GetString("title")
	content := c.GetString("content")
	typeId, _ := c.GetInt("type_id")
	cover := c.GetString("cover")
	remark := c.GetString("remark")
	if title == "" || content == "" || typeId == 0 || cover == "" {
		c.Data["json"] = map[string]interface{}{
			"status":  500,
			"message": "参数不能为空",
		}
	}
	//news := models.News{
	//	Title:   title,
	//	Content: content,
	//	TypeID:  typeId,
	//	Cover:   cover,
	//	Remark:  remark,
	//}
	var news models.News
	news.Title = title
	news.Content = content
	news.TypeID = typeId
	news.Cover = cover
	news.Remark = remark
	if err = news.Create(); err != nil {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"msg":  "系统错误",
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"msg":  "success",
	}
	c.ServeJSON()
	return
}
func (c *NewsController) List() {
	//var err error
	//keywork := c.GetString("keywork")

}
